//
//  RepairCell.m
//  HAODE
//
//  Created by panquanReabam.com on 15/10/14.
//  Copyright (c) 2015年 reabam.com. All rights reserved.
//

#import "RepairCell.h"

#import "UIButton+WebCache.h"
#import "HAODEsingleton.h"
#import "HttpRequest.h"
#import "TheLoginViewController.h"
#import "MJPhotoBrowser.h"
#import "MJPhoto.h"
@interface RepairCell ()<UICollectionViewDataSource,UICollectionViewDelegate>
{
    NSArray *array;
    NSInteger collectionHeight;
    UIView *containerView;
    UICollectionView *collection;
    HAODEsingleton*commdity;
    int is;
    UIImageView*images;
}
@end
@implementation RepairCell{
    
    
}

- (void)awakeFromNib {
    
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _imageArray.count;
}

//- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
//{
//    static NSString *str = @"collection";
//    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:str forIndexPath:indexPath];
//    for (UIView *view in [cell.contentView subviews]) {
//        [view removeFromSuperview];
//    }
//    UIButton* image=[[UIButton alloc]initWithFrame:CGRectMake(0, 0, cell.width, cell.height)];
//    
//   [image setImageWithURL:[NSURL URLWithString:_imageArray[indexPath.row]]  placeholderImage:[UIImage imageNamed:@"logo_normalss.png"]];
////    [image xr_setButtonImageWithUrl:_imageArray[indexPath.row]];
//    image.userInteractionEnabled=NO;
//    
//    [cell addSubview:image];
//
//    return cell;
//}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *str = @"collection";
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:str forIndexPath:indexPath];
    for (UIView *view in [cell.contentView subviews]) {
        [view removeFromSuperview];
    }
    
    images=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, cell.width, cell.height)];
    [images setImageWithURL:[NSURL URLWithString:_imageArray[indexPath.row]]  placeholderImage:[UIImage imageNamed:@"logo_normalss.png"]];
    images.tag = indexPath.row;
    
    images.userInteractionEnabled = YES;
    [images addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapImage:)]];
    
    // 内容模式
    images.clipsToBounds = YES;
    images.contentMode = UIViewContentModeScaleAspectFill;
    [cell addSubview:images];
    
    return cell;
}
-(BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    
    return YES;
}
//- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath{
//
//
//
//}

- (void)tapImage:(UITapGestureRecognizer *)tap
{
    int count = _imageArray.count;
    // 1.封装图片数据
    NSMutableArray *photos = [NSMutableArray arrayWithCapacity:count];
    for (int i = 0; i<count; i++) {
        // 替换为中等尺寸图片
        NSString *url = [_imageArray[i] stringByReplacingOccurrencesOfString:@"thumbnail" withString:@"bmiddle"];
        MJPhoto *photo = [[MJPhoto alloc] init];
        photo.url = [NSURL URLWithString:url]; // 图片路径
        
        //        photo.srcImageView = image.subviews[i]; // 来源于哪个UIImageView
        [photos addObject:photo];
    }
    
    // 2.显示相册
    MJPhotoBrowser *browser = [[MJPhotoBrowser alloc] init];
    browser.currentPhotoIndex = tap.view.tag; // 弹出相册时显示的第一张图片是？
    browser.photos = photos; // 设置所有的图片
    [browser show];
}
#define kYOfImageContainer   136.0

#define kYOfImageContainer   136.0


- (void)setComment:(Repairmodel *)comment
{
    commdity=[HAODEsingleton shared];
    [containerView removeFromSuperview];
       
    _comment = comment;
    _imageArray = comment.imagePicBig;
    if ([commdity.anniusat isEqualToString:@"you"]) {
       
        
        
    }else{
    
    
    }
    commdity.orderId=comment.reqId;
    _image.layer.masksToBounds = YES;
    _image.layer.cornerRadius = 35/2;
    _image.layer.borderColor = [UIColor whiteColor].CGColor;
    _image.layer.borderWidth = 0;
    _image.contentMode = UIViewContentModeScaleAspectFit;
    _image.userInteractionEnabled = YES;
    
    self.diselabelview.layer.borderWidth = 0.30;
    self.diselabelview.layer.borderColor = qianhuicolor.CGColor;
    self.nameLabel.text = comment.userName;
    self.dateLabel.text = comment.createdAt;
    self.time.text = comment.createTime;
    
    
    if ([comment.status isEqualToString:@"Y"]) {
        self.dateLabel.text =[NSString stringWithFormat:@"%@" , @"已处理"];
        self.dateLabel.font=[UIFont systemFontOfSize:15];
        self.dateLabel.textColor=luseColor;
        
        
    }
    if ([comment.status isEqualToString:@"N"]) {
        self.dateLabel.text =[NSString stringWithFormat:@"%@" , @"未处理"];
        self.dateLabel.font=[UIFont systemFontOfSize:15];
        self.dateLabel.textColor=hongsecolor;
        [self .baojiaBtn.layer setCornerRadius:4.0]; //设置矩圆角半径
        [self .baojiaBtn.layer setBorderWidth:0.5];   //边框宽度
        CGColorSpaceRef tureBTnSpace = CGColorSpaceCreateDeviceRGB();
        CGColorRef tureBTncolorref = CGColorCreate(tureBTnSpace,(CGFloat[]){23/255.0, 148/255.0, 153/255.0, 1.0});
        [self .baojiaBtn.layer setBorderColor:tureBTncolorref];//边框颜色
        [self .baojiaBtn setTitleColor:luseColor forState:UIControlStateNormal];
        
        [self.baojiaBtn setTitle:@"催促跟进" forState:UIControlStateNormal];
        
        [self.baojiaBtn addTarget:self action:@selector(cui:) forControlEvents:UIControlEventTouchUpInside];
        
    }
    if ([comment.status isEqualToString:@"W"]) {
        self.dateLabel.text =[NSString stringWithFormat:@"%@" , @"处理中"];
        self.dateLabel.font=[UIFont systemFontOfSize:15];
        self.dateLabel.textColor=luseColor;
        [self .baojiaBtn.layer setCornerRadius:4.0]; //设置矩圆角半径
        [self .baojiaBtn.layer setBorderWidth:0.5];   //边框宽度
        CGColorSpaceRef tureBTnSpace = CGColorSpaceCreateDeviceRGB();
        CGColorRef tureBTncolorref = CGColorCreate(tureBTnSpace,(CGFloat[]){23/255.0, 148/255.0, 153/255.0, 1.0});
        [self .baojiaBtn.layer setBorderColor:tureBTncolorref];//边框颜色
        [self .baojiaBtn setTitleColor:luseColor forState:UIControlStateNormal];
        
        [self.baojiaBtn setTitle:@"催促跟进" forState:UIControlStateNormal];
        
        [self.baojiaBtn addTarget:self action:@selector(cui:) forControlEvents:UIControlEventTouchUpInside];
        
    }
    if ([comment.status isEqualToString:@"C"]) {
        self.dateLabel.text =[NSString stringWithFormat:@"%@" , @"已取消"];
        self.dateLabel.font=[UIFont systemFontOfSize:15];
        self.dateLabel.textColor=qianhuicolor;
        [self .baojiaBtn.layer setCornerRadius:4.0]; //设置矩圆角半径
        [self .baojiaBtn.layer setBorderWidth:0.5];   //边框宽度
        CGColorSpaceRef tureBTnSpace = CGColorSpaceCreateDeviceRGB();
        CGColorRef tureBTncolorref = CGColorCreate(tureBTnSpace,(CGFloat[]){188/255.0, 188/255.0, 188/255.0, 1.0});
        [self .baojiaBtn.layer setBorderColor:tureBTncolorref];//边框颜色
        [self .baojiaBtn setTitleColor:qianhuicolor forState:UIControlStateNormal];
        
        [self.baojiaBtn setTitle:@"催促跟进" forState:UIControlStateNormal];
        
//        [self.baojiaBtn addTarget:self action:@selector(cui:) forControlEvents:UIControlEventTouchUpInside];
        
    }
   
        
        
        
    
            NSDictionary *attrbute = @{NSFontAttributeName:[UIFont systemFontOfSize:15]};
        CGFloat textHeight;
        if ([comment.details isEqualToString:@""]) {
            textHeight = 5;
        }else{
            textHeight = [[NSString stringWithFormat:@"%@" ,comment.details]
                          boundingRectWithSize:CGSizeMake(ScreenWidth-40, 1000) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:attrbute context:nil].size.height +15;
        }

        
        self.conten.text =[NSString stringWithFormat:@"%@" , comment.details];
        self.conten.font=[UIFont systemFontOfSize:15];
        self.conten.textColor=shenhuicolor;
        
     
        
        
 
      [collection removeFromSuperview];
    
    if (comment.imagePicSmall>0) {
        
        
        
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
        //设置单元格的尺寸
        flowLayout.itemSize = CGSizeMake(collectionCellHeight, collectionCellHeight);
        
        flowLayout.minimumInteritemSpacing = 5;
        flowLayout.minimumLineSpacing = 5;
        flowLayout.sectionInset = UIEdgeInsetsMake(15, 15, 15, 15);
        
        collection = [[UICollectionView alloc] initWithFrame:CGRectMake(0,textHeight+50+10, ScreenWidth, collectionCellHeight+15*2) collectionViewLayout:flowLayout];
        collection.backgroundColor = [UIColor clearColor];
        collection.dataSource=self;
        collection.delegate=self;
        
        [collection registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"collection"];
        
        
        int numberOfLines = (int)ceil([self collectionView:collection numberOfItemsInSection:0] / 4.0);
        
        CGRect frame = collection.frame;
        frame.size.height = numberOfLines *collectionCellHeight + (numberOfLines - 1) * 4 + 15*2;
        collection.frame = frame;
        
        [self.contentView addSubview:collection];
        
        
    }

//    if (comment.imagePicSmall.count > 0) {
//      
//        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
//        layout.itemSize = CGSizeMake((ScreenWidth-45)/4 , (ScreenWidth-45)/4);
//        layout.minimumInteritemSpacing = 1;
//        layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
//        
//
//        collectionHeight = (comment. imagePicSmall.count/4 +1) *(layout.itemSize.width + 10) -10;
//        
//        collection = [[UICollectionView alloc]initWithFrame:CGRectMake(12.5, textHeight+50+15, ScreenWidth-25, collectionHeight) collectionViewLayout:layout];
//        collection.backgroundColor = [UIColor clearColor];
//
//        [collection registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"collection"];
//        collection.delegate = self;
//        collection.dataSource = self;
//        [self.contentView addSubview:collection];
//    }
    UILabel*line=[[UILabel alloc]initWithFrame:CGRectMake(15, 50, ScreenWidth-30, 0.35)];
    line.backgroundColor=LineColor;
    [self.contentView addSubview:line];
    
   
}

-(void)cui:(UIButton*)sender{



    NSUserDefaults *user1 = [NSUserDefaults standardUserDefaults];
    NSString* tokenId = [user1 objectForKey:@"tokenId"];
    NSMutableDictionary *params = [NSMutableDictionary new];
    [params setValue:tokenId forKey:@"tokenId"];
    [params setValue:commdity.orderId forKey:@"demandId"];
    
    NSLog(@"-----%@",params);
   
    [HttpRequest requestData:@"/Estate/Demand/Repair/Urge" params:params success:^(BOOL isSuccess, SeverData *severDataDict) {
        if (isSuccess) {
            
            NSLog(@"-----%@",severDataDict.dataDict);
            
            if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==0) {
              
                NSString *requestResultStr=[severDataDict.dataDict objectForKey:@"ResultString"];
                [Global showTextHudSub:requestResultStr view:self.contentView];
              
                
                
                
                
                
            }else{
                
                
                NSString *requestResultStr=[severDataDict.dataDict objectForKey:@"ResultString"];
                [Global showTextHudSub:requestResultStr view:self.contentView];
                
                
            }
            
        }
        else
        {
           
        }
        
    }];





}






+ (CGFloat)cellHeight:(Repairmodel *)commentModel
{
    // 手动计算一下containerView的高度，拿此高度再加其它控件的高度
    NSDictionary *attrbute = @{NSFontAttributeName:[UIFont systemFontOfSize:15]};
    CGFloat textHeight;
    if ([commentModel.details isEqualToString:@""]) {
        textHeight = 5;
    }else{
        textHeight = [[NSString stringWithFormat:@"%@" ,commentModel.details]
                      boundingRectWithSize:CGSizeMake(ScreenWidth-40, 1000) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:attrbute context:nil].size.height +15;
    }
    
    
    CGFloat headImageHeight = 110;
    
    //collectionView height
    NSInteger arrayCount = commentModel.imagePicSmall.count;
    int i = 1;
    if (arrayCount % 5 == 0) {
        i++;
    }
    CGFloat collectionH = (i) *((ScreenWidth-50)/4 + 10) ;
    
    return  headImageHeight + textHeight +25 + collectionH;
}


@end
